"""
Case Type   : HTAP行列融合特性
Case Name   : 列缓存查询开关enable_imcsscan设置方式验证
Create At   : 2024/12/25
Owner       : ningyali
Description :
    1.gs_guc set+重启
    2.gs_guc reload无需重启
    3.gsql中SET paraname TO value;
    4.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.values = {'on': True, 'off': True, '1': True, '0': True,
                       'null': False, '\'\'': False, '1.1': False,
                       '123': False, '\'*&^%$#@\'': False, '\'熊猫\'': False}

    def test_trans(self):
        text = '----step1:gs_guc set+重启;expect:执行成功----'
        self.log.info(text)
        for param, expect in self.values.items():
            guc_res = self.pri_sh.execute_gsguc(
                'set', self.Constant.GSGUC_SUCCESS_MSG,
                f'enable_imcsscan={param}')
            self.assertEqual(expect, guc_res, f'参数配置失败:{text}')

            if param in ('on', 'off'):
                is_restarted = self.pri_sh.restart_db_cluster()
                self.assertTrue(is_restarted, f'重启数据库失败:{text}')

                sql = 'show enable_imcsscan;'
                sql_res = self.pri_sh.execut_db_sql(
                    sql, env_path=macro.SINGLE_DB_ENV_PATH)
                self.assertIn(param, sql_res, f'连库查询值失败:{text}')

        text = '----step2:gs_guc reload无需重启;expect:执行成功----'
        self.log.info(text)
        for param, expect in self.values.items():
            guc_res = self.pri_sh.execute_gsguc(
                'reload', self.Constant.GSGUC_SUCCESS_MSG,
                f'enable_imcsscan={param}')
            self.assertEqual(expect, guc_res, f'参数配置失败:{text}')

            if param in ('on', 'off'):
                sql = 'show enable_imcsscan;'
                sql_res = self.pri_sh.execut_db_sql(
                    sql, env_path=macro.SINGLE_DB_ENV_PATH)
                self.assertIn(param, sql_res, f'连库查询值失败:{text}')

        text = '----step3:gsql中SET paraname TO value;expect:执行成功----'
        self.log.info(text)
        for param in self.values.keys():
            sql = f'set enable_imcsscan to {param};' \
                  f'show enable_imcsscan;'
            sql_res = self.pri_sh.execut_db_sql(
                sql, env_path=macro.SINGLE_DB_ENV_PATH)
            rows, _ = self.common.parse_sql_query_res(sql_res)
            self.log.info(f'res:{rows}')
            if param in ('on', 'off'):
                self.assertEqual([[param]], rows, f'连库查询值失败:{text}')

    def tearDown(self):
        text = '----step4:清理环境;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_imcsscan')
        is_restarted = self.pri_sh.restart_db_cluster()

        self.assertTrue(guc_res, f'参数配置失败:{text}')
        self.assertTrue(is_restarted, f'重启数据库失败:{text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
